package nl.hr.minor.mobilelife.j1213.jlr.findit;

import nl.hr.minor.mobilelife.j1213.jlr.R;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Activity to display a summary of the end of all levels.
 * Displays the amount of right and wrong guesses for every level.
 * @author Leon
 *
 */
public class Scores extends Activity implements View.OnClickListener{

	/**
	 * Keys to save/fetch the scores and mistakes from SharedPreferences
	 */
	public static final String SCORE = "com.example.minigames.SCORE";
	public static final String MISTAKES = "com.example.minigames.MISTAKES";

	private int totalScore;
	
	private TextView scoreLevelOne;
	private TextView mistakesLevelOne;
	
	private TextView scoreLevelTwo;
	private TextView mistakesLevelTwo;
	
	private TextView scoreLevelThree;
	private TextView mistakesLevelThree;
	
	private TextView scoreLevelFour;
	private TextView mistakesLevelFour;
	
	private TextView finalScoreLevelOne;
	private TextView finalScoreLevelTwo;
	private TextView finalScoreLevelThree;
	private TextView finalScoreLevelFour;
	
	private TextView totalScoreValue;

	// edittext and button to save the score to the database
	private EditText nickname;
	private Button saveButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.findit_score);
		initVars();
		setScores();
	}

	/**
	 * Initializes all TextViews to display the scores
	 */
	private void initVars() {
		scoreLevelOne = (TextView) findViewById(R.id.scoreLevelOne);
		mistakesLevelOne = (TextView) findViewById(R.id.mistakesLevelOne);
		finalScoreLevelOne = (TextView) findViewById(R.id.finalScoreLevelOne);
		
		scoreLevelTwo = (TextView) findViewById(R.id.scoreLevelTwo);
		mistakesLevelTwo = (TextView) findViewById(R.id.mistakesLevelTwo);
		finalScoreLevelTwo = (TextView) findViewById(R.id.finalScoreLevelTwo);
		
		scoreLevelThree =  (TextView) findViewById(R.id.scoreLevelThree);
		mistakesLevelThree = (TextView) findViewById(R.id.mistakesLevelThree);
		finalScoreLevelThree = (TextView) findViewById(R.id.finalScoreLevelThree);
		
		scoreLevelFour = (TextView) findViewById(R.id.scoreLevelFour);
		mistakesLevelFour = (TextView) findViewById(R.id.mistakesLevelFour);
		finalScoreLevelFour = (TextView) findViewById(R.id.finalScoreLevelFour);
		
		totalScoreValue = (TextView) findViewById(R.id.totalScoreValue);
		
		nickname = (EditText) findViewById(R.id.nickname);
		saveButton = (Button) findViewById(R.id.button);
		
		saveButton.setOnClickListener(this);
	}

	/**
	 * Fetches the scores and mistakes from the SharedPreferences. All scores were saved into the SharedPreferences
	 */
	private void setScores() {

		SharedPreferences sharedPreferences;

		int score;
		int mistakes;
		int levelScore;

		// keys used to identify all scores per level
		String[] level = {	SearchDifferenceOne.LEVEL, SearchDifferenceTwo.LEVEL, 
				SearchDifferenceThree.LEVEL, SearchDifferenceFour.LEVEL};

		// fetch all guesses and mistakes and calculate the final score
		for(int i = 0; i < 4; i++){
			sharedPreferences = getSharedPreferences(level[i], Activity.MODE_PRIVATE);
			score = sharedPreferences.getInt(SCORE, 0);
			mistakes = sharedPreferences.getInt(MISTAKES, 0);

			if(score > mistakes)
				levelScore = score - mistakes;
			else
				levelScore = 0;

			totalScore += levelScore; 

			switch(i){
			case 0:
				scoreLevelOne.setText("Tijd score: "+score);
				mistakesLevelOne.setText("foutjes: "+mistakes);
				finalScoreLevelOne.setText("Totaal score: "+levelScore);
				break;
			case 1:
				scoreLevelTwo.setText("Tijd score: "+score);
				mistakesLevelTwo.setText("foutjes: "+mistakes);
				finalScoreLevelTwo.setText("Totaal score: "+levelScore);
				break;
			case 2:
				scoreLevelFour.setText("Tijd score: "+score);
				mistakesLevelFour.setText("foutjes: "+mistakes);
				finalScoreLevelFour.setText("Totaal score: "+levelScore);
				break;
			case 3:
				scoreLevelThree.setText("Tijd score: "+score);
				mistakesLevelThree.setText("foutjes: "+mistakes);
				finalScoreLevelThree.setText("Totaal score: "+levelScore);
				break;
			}
		}
		totalScoreValue.setText(""+totalScore);
	}

	@Override
	public void onClick(View v) {
		String nicknameText = nickname.getText().toString();

		boolean succeeded = true;
		if(nicknameText != null){
			try{
				Database entry = new Database(this);
				entry.open();
				entry.insert(nicknameText, totalScore);
				entry.close();
			}catch(Exception e){
				succeeded = false;
				Dialog dialog = new Dialog(this);
				dialog.setTitle("database insert mislukt");
				TextView textView = new TextView(this);
				textView.setText("score niet geplaatst!");
				dialog.setContentView(textView);
				dialog.show();
			}finally{
				if(succeeded){
					Intent intent = new Intent(this, HighScores.class);
					startActivity(intent);
					finish();
				}
			}
		}
	}
}
